package com.lanhao.aot.iot;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.function.Function;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.util.CollectionUtils;

import com.lanhao.aot.sharding.CommonPreciseShardingAlgorithm;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;

public class RoomMakeupUtil {
	public static final String sourceFilePath = "D:/makeup/iotAccount.txt"; // IOT_ACCOUNT文件列表的位置
	public static final String resultFilePath = "D:/makeup/resultSQL.txt"; // 最终生成的SQL脚本
	public static final String LINE_SEPARATOR = "\r\n";

	public static void main(String[] args) {
		File dataFile = new File(sourceFilePath);
		if (!dataFile.exists()) {
			throw new RuntimeException("用户数据文件不存在");
		}
		List<String> iotAccountList = FileUtil.readLines(dataFile, Charset.forName("UTF-8"));
		if (CollectionUtils.isEmpty(iotAccountList)) {
			throw new RuntimeException("用户数据文件为空");
		}
		File resultFile = new File(resultFilePath);
		int totalSize = iotAccountList.size();
		MutableInt counter = new MutableInt(0);
		iotAccountList.forEach(param -> {
			if (StringUtils.isNotBlank(param)) {
				Pair<String, String> apply = generatesQlFunc.apply(param);
				int currentIndex = counter.addAndGet(1);
				System.out.println("总共有" + totalSize + "条记录，当前已生成" + currentIndex + "个");
				FileUtil.appendString(apply.getRight(), resultFile, Charset.forName("UTF-8"));
			}
		});
	}

	static Function<String, Pair<String, String>> generatesQlFunc = param -> {
		String houseId = UUID.fastUUID().toString().replace("-", "");
		String roomId1 = UUID.fastUUID().toString().replace("-", "");
		String roomId2 = UUID.fastUUID().toString().replace("-", "");
		String roomId3 = UUID.fastUUID().toString().replace("-", "");

		String iotAccount = param.split("_")[0];
		String deviceId = param.split("_")[1];

		int roomTableIndex = CommonPreciseShardingAlgorithm.getShardingIndex(32, iotAccount);
		int houseTableIndex = CommonPreciseShardingAlgorithm.getShardingIndex(16, iotAccount);
		StringBuilder houseTemplate = new StringBuilder().append(
				"insert into t_house_info_{}(id, iot_account, name, house_type, state, create_time, update_time, default_scene) values ")
				.append("('{}','{}','我的家','0','1',now(), now(), '1');").append(LINE_SEPARATOR);

		StringBuilder roomTemplate1 = new StringBuilder().append(
				"insert into t_room_info_{}(id, iot_account, name, house_id, room_type, state, create_time, update_time) values ")
				.append("('{}','{}','默认房间','{}','0','1',now(),now());").append(LINE_SEPARATOR);

		StringBuilder roomTemplate2 = new StringBuilder().append(
				"insert into t_room_info_{}(id, iot_account, name, house_id, room_type, state, create_time, update_time) values ")
				.append("('{}','{}','客厅','{}','1','1',now(),now());").append(LINE_SEPARATOR);

		StringBuilder roomTemplate3 = new StringBuilder().append(
				"insert into t_room_info_{}(id, iot_account, name, house_id, room_type, state, create_time, update_time) values ")
				.append("('{}','{}','卧室','{}','1','1',now(),now());").append(LINE_SEPARATOR);

		StringBuilder deviceDynamicTemplate = new StringBuilder()
				.append("UPDATE t_device_dynamic_info SET house_id='{}',room_id='{}' WHERE device_id='{}';")
				.append(LINE_SEPARATOR);

		String housesQl = StrUtil.format(houseTemplate, houseTableIndex, houseId, iotAccount);
		String roomsQl1 = StrUtil.format(roomTemplate1, roomTableIndex, roomId1, iotAccount, houseId);
		String roomsQl2 = StrUtil.format(roomTemplate2, roomTableIndex, roomId2, iotAccount, houseId);
		String roomsQl3 = StrUtil.format(roomTemplate3, roomTableIndex, roomId3, iotAccount, houseId);

		String deviceDynamicsQl = StrUtil.format(deviceDynamicTemplate, houseId, roomId1, deviceId);

		System.out.println("hset iot:d:dy:" + deviceId + " houseId " + houseId);
		System.out.println("hset iot:d:dy:" + deviceId + " roomId " + roomId1);
		return Pair.of(param, housesQl + roomsQl1 + roomsQl2 + roomsQl3 + deviceDynamicsQl);
	};

}
